﻿using RFIDSystem.Common.HttpContextUser;
using RFIDSystem.IServices;
using RFIDSystem.Model.Models;
using RFIDSystem.Model.ViewModels;
using RFIDSystem.Services.BASE;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RFIDSystem.Services
{
    public class ElectronicFenceServices : BaseServices<ElectronicFence>, IElectronicFenceServices
    {
        private readonly IUser _user;

        public ElectronicFenceServices(IUser user)
        {
            _user = user;
        }

        public async Task<bool> AddElectronicFence(ElectronicFence input)
        {
            await base.Add(input);

            return true;
        }
        public async Task<bool> EditElectronicFence(ElectronicFence input)
        {
            var ef = await base.QueryById(input.Id);
            if (ef != null)
            {
                ef.Name = input.Name;
                ef.Level = input.Level;
                await base.Update(ef);
            }


            return true;
        }

        public async Task<bool> DelElectronicFence(string id)
        {
            await base.DeleteById(id);

            return true;
        }

        public async Task<List<ElectronicFence>> GetElectronicFences()
        {
            List<ElectronicFence> rs = new List<ElectronicFence>();
            var ef = await base.Query();

            return ef;
        }
        public async Task<List<ElectronicFence>> GetMyElectronicFences()
        {
            List<ElectronicFence> rs = new List<ElectronicFence>();
            var ef = await base.QueryMuch<UserElectronicFence,ElectronicFence,ElectronicFence>((uef,ef)=>new object []{JoinType.Left,uef.ElectronicFenceId==ef.Id}, (uef, ef) =>new ElectronicFence { Id=ef.Id,Name=ef.Name, Level=ef.Level,Polygon=ef.Polygon}, (uef, ef) =>uef.UserId== _user.ID);

            return ef;
        }
    }
}
